/*
 * @Author: Starfish_DL
 * @Date: 2020-10-19 11:54:41
 * @LastEditors: Starfish_DL
 * @LastEditTime: 2020-10-26 17:26:19
 * @CurrentVersion: 1.0
 * @Description: 
 */

#include<iostream>
#include<string>
#define MAXSIZE 50
using namespace std;

class SqString
{
private:
    char data[MAXSIZE];
    int length;
public:
    SqString();
    SqString(string str);
    SqString(string str,unsigned int n);
    void SqStrCopy(string str);
    void SqStrCopy(SqString str);
    bool SqStrEqual(SqString str);
    int SqStrLength();
    void SqConcat(string str);
    void SqConcat(SqString str);
    string SqSubStr(unsigned int a,unsigned int b);
    void SqInsStr(unsigned int i,string str);
    void SqInsStr(unsigned int i,SqString str);
    string SqDelStr(unsigned int a,unsigned int b);
    void SqRepStr(unsigned int a,unsigned int b,string str);
    void SqRepStr(unsigned int a,unsigned int b,SqString str);
    void SqDispStr();
    SqString operator+(const SqString &str);
    ~SqString();
};

SqString::SqString()
{
    this->length=0;
}

SqString::SqString(string str)
{
    int i=0;
    while (str[i]!='\0' && i<MAXSIZE)
    {
        this->data[i]=str[i];
        i++;
    }
    this->length=i;
}

SqString::SqString(string str,unsigned int n)
{
    int i=0;
    while (i<n && i<MAXSIZE)
    {
        this->data[i]=str[i];
        i++;
    }
    this->length=i;
}

SqString::~SqString()
{
}

void SqString::SqStrCopy(string str)
{
    int i=0;
    while (str[i]!='\0' && i<MAXSIZE)
    {
        this->data[i]=str[i];
        i++;
    }
    this->length=i;
}

void SqString::SqStrCopy(SqString str)
{
    int i=0;
    while (i<str.length)
    {
        this->data[i]=str.data[i];
        i++;
    }
    this->length=str.length;    
}

void SqString::SqConcat(string str)
{
    int i=0;
    while (str[i]!='\0' && i+this->length<MAXSIZE)
    {
        this->data[i+this->length]=str[i];
        i++;
    }
    this->length=this->length+i;
}

void SqString::SqConcat(SqString str)
{
    int i=0;
    while (i<str.length && i+this->length<MAXSIZE)
    {
        this->data[i+this->length]=str.data[i];
        i++;
    }
    this->length=this->length+i;
}

string SqString::SqDelStr(unsigned int a,unsigned int b)
{
    if (b<a)
    {
        int temp=a;
        a=b;
        b=temp;
    }
    if (a>=this->length)
    {
        return "";
    }
    else if (a<this->length && b>=this->length)
    {
        string temp="";
        int i=a;
        while (i<this->length)
        {
            temp.append(1,this->data[i]);
            i++;
        }
        this->length=a;
        return temp;
    }
    else
    {
        string temp="";
        int i=a;
        while (i<=b)
        {
            temp.append(1,this->data[i]);
            i++;
        }
        int t=a;
        int k=b+1;
        while (k<this->length)
        {
            this->data[t]=this->data[k];
            t++;
            k++;
        }
        this->length=this->length-(b-a+1);
        return temp;
    }
}

// SqString SqString::SqDelStr(unsigned int a,unsigned int b)
// {
//     if (b<a)
//     {
//         int temp=a;
//         a=b;
//         b=temp;
//     }
//     if (a>=this->length)
//     {
//         SqString s=new SqString();
//         return s;
//     }
//     else if (a<this->length && b>=this->length)
//     {
//         int i=a;
//         SqString s=new SqString();
//         s.length=this->length-a;
//         while (i<this->length)
//         {
//             s.data[i-s.length]=this->data[i];
//             i++;
//         }
//         this->length=a;
//         return s;
//     }
//     else
//     {
//         int t=a;
//         int k=b+1;
//         while (k<this->length)
//         {
//             this->data[t]=this->data[k];
//             t++;
//             k++;
//         }
//         this->length=this->length-(b-a+1);
//         SqString s=new SqString();
//         s.length=b-a+1;
//         while (a<=b)
//         {
//             s.data[a-s.length]=this->data[a];
//             a++;
//         }
//         return s;
//     }
// }

void SqString::SqDispStr()
{
    for (int i = 0; i < this->length; i++)
    {
        cout<<this->data[i]<<" ";
    }
    cout<<endl;
}

void SqString::SqInsStr(unsigned int i,string str)
{
    if (i>this->length)
    {
        return;
    }
    else
    {
        for (int k = this->length-1; k >= i; k--)
        {
            if (k+str.length()>=MAXSIZE)
            {
                continue;
            }
            this->data[k+str.length()]=this->data[k];
        }
        int a=this->length;
        while (str[a-this->length]!='\0' && a<MAXSIZE)
        {
            this->data[i]=str[a-this->length];
            i++;a++;
        }
        this->length=a;
        return;
    }
}

void SqString::SqInsStr(unsigned int i,SqString str)
{
    if (i>this->length)
    {
        return;
    }
    else
    {
        for (int k = this->length-1; k >= i; k--)
        {
            if (k+str.length>=MAXSIZE)
            {
                continue;
            }
            this->data[k+str.length]=this->data[k];
        }
        int a=this->length;
        while (a-this->length<str.length && a<MAXSIZE)
        {
            this->data[i]=str.data[a-this->length];
            i++;a++;
        }
        this->length=a;
        return;
    }    
}

void SqString::SqRepStr(unsigned int a,unsigned int b,string str)
{
    this->SqDelStr(a,b);
    this->SqInsStr(a,str);
}

bool SqString::SqStrEqual(SqString str)
{
    if (this->length!=str.length)
    {
        return false;
    }
    int i=0;
    while (i<this->length)
    {
        if (this->data[i]!=str.data[i])
        {
            return false;
        }
    }
    return true;
}

int SqString::SqStrLength()
{
    return this->length;
}

string SqString::SqSubStr(unsigned int a,unsigned int b)
{
    if (b<a)
    {
        int temp=a;
        a=b;
        b=temp;
    }
    if (a>=this->length)
    {
        return "";
    }
    else if (a<this->length && b>=this->length)
    {
        string temp="";
        int i=a;
        while (i<this->length)
        {
            temp.append(1,this->data[i]);
            i++;
        }
        return temp;
    }
    else
    {
        string temp="";
        int i=a;
        while (i<=b)
        {
            temp.append(1,this->data[i]);
            i++;
        }
        return temp;
    }
}

SqString SqString::operator+(const SqString &str) 
{
    SqString StrNew=*this;
    StrNew.SqInsStr(this->length,str);
    return StrNew;
}

int main()
{
    string str1="ThisIsASq6yyt"; //length=13
    string str2="Thiragh55";
    SqString s1(str1);
    SqString s2(str2);
    s1=s1+s2;
    s1.SqDispStr();
    return 0;
}